package itcz.example.pattern.create_action.singleton.demo8;


import java.io.*;

/**
 * @ClassName: Client
 * @Author: ITCZ
 * @Date: 2025-04-21
 * @Description: 序列化和反系列化破坏单例模式
 */
public class Client {
    public static void main(String[] args) throws Exception {
        writeObject2File();
        readObjectFromFile();
        readObjectFromFile();
    }

    public static void writeObject2File() throws Exception {
        // 1.获取对象
        Singleton singleton = Singleton.getInstance();
        // 创建对象输出流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Users/itcz/Desktop/a.txt"));
        // 写对象
        oos.writeObject(singleton);
        oos.close();
    }

    public static void readObjectFromFile() throws Exception {
        // 创建对象输入流对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/Users/itcz/Desktop/a.txt"));
        // 读对象
        Singleton singleton = (Singleton) ois.readObject();
        System.out.println(singleton);
        ois.close();
    }
}
